package edu.bbu.bibliospring.backend.service.beans;

import edu.bbu.bibliospring.backend.model.Book;
import edu.bbu.bibliospring.backend.repository.BookRepository;
import edu.bbu.bibliospring.backend.repository.exception.BibliospringRepositoryException;
import edu.bbu.bibliospring.backend.service.BookService;
import edu.bbu.bibliospring.backend.service.exception.BibliospringServiceException;

import javax.ejb.DependsOn;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Created by anna on 6/12/14.
 */

@Stateless(name = "BookService", mappedName = "ejb/BookService")
@DependsOn({"BookRepository", "Book"})
public class BookServiceBean implements BookService {

    private static final Logger LOG = Logger.getLogger(BookService.class.getName());

    @EJB
    private BookRepository bookRepository;

    @Override
    public List<Book> getAll() throws BibliospringServiceException {
        try {
            LOG.info("BookService - getAll");
            return bookRepository.getAll();
        } catch (BibliospringRepositoryException e) {
            LOG.log(Level.SEVERE, e.getMessage());
            throw new BibliospringServiceException("Book repository - getAll()", e);
        }
    }

    @Override
    public Book getById(Long id) throws BibliospringServiceException {
        try {
            LOG.info("BookService - getById");
            return bookRepository.getById(id);
        } catch (BibliospringRepositoryException e) {
            LOG.log(Level.SEVERE, e.getMessage());
            throw new BibliospringServiceException("Book repository - getById()", e);
        }
    }

    @Override
    public Book insertBook(Book book) throws BibliospringServiceException {
        try {
            LOG.info("BookService - insertBook");
            return bookRepository.save(book);
        } catch (BibliospringRepositoryException e) {
            LOG.log(Level.SEVERE, e.getMessage());
            throw new BibliospringServiceException("Book repository - insertBook()", e);
        }
    }
}
